
import java.net.*;
import java.util.Random;

public class BomberServer extends Thread {

    private User[] users;
    private Connector[] connectors;
    private final int SIZE = 2;
    private final int W = 640 + (16 * SIZE);
    private final int H = 480;
    private Integer[][] walk;
    private final int[][] POS = {{0, 0}, {W - (16 * SIZE), 0}, {W - (16 * SIZE), H - (16 * SIZE)}, {0, H - (16 * SIZE)}};

    public static void main(String[] args) {
        new BomberServer();
    }

    public BomberServer() {
        users = new User[4];
        connectors = new Connector[4];
        walk = createWalk();
        try {
            ServerSocket server = new ServerSocket(12345);
            while (true) {
                Socket socket = server.accept();
                addUser(socket);
            }
        } catch (Exception e) {
        }
    }

    public void disconnect(int id) {
        users[id] = null;
        connectors[id] = null;
        
        int[] all = {4, id};
        sendAll(all);
    }

    private Integer[][] walk() {
        return walk;
    }

    private void addUser(Socket socket) {
        for (int i = 0; i < users.length; i++) {
            if (users[i] == null) {
                users[i] = new User(i, POS[i][0], POS[i][1], 8, 1);
                connectors[i] = new Connector(this, socket, i);

                int[] all = {0, SIZE};
                connectors[i].send(all);

                connectors[i].sendObject(1, walk);

                connectors[i].sendObject(2, users[i]);

                for (int j = 0; j < 4; j++) {
                    if (i != j && users[j] != null) {
                        connectors[i].sendObject(2, users[j]);
                        connectors[j].sendObject(2, users[i]);
                    }
                }

                all[0] = 3;
                all[1] = i;
                connectors[i].send(all);
                break;
            }
        }
    }

    private Integer[][] createWalk() {
        Integer[][] walk = new Integer[W / (16 * SIZE)][H / (16 * SIZE)];
        for (int i = 0; i < walk.length; i++) {
            for (int j = 0; j < walk[i].length; j++) {
                walk[i][j] = 0;
            }
        }

        Random ran = new Random();

        for (int i = 0; i < walk.length; i++) {
            for (int j = 0; j < walk[i].length; j++) {
                if (ran.nextBoolean()) {
                    walk[i][j] = 2;
                }
            }
        }

        for (int i = 1; i < walk.length; i += 2) {
            for (int j = 1; j < walk[i].length; j += 2) {
                walk[i][j] = 1;
            }
        }
        walk[0][0] = 0;
        walk[0][1] = 0;
        walk[1][0] = 0;

        walk[0][walk[0].length - 2] = 0;
        walk[0][walk[0].length - 1] = 0;
        walk[1][walk[0].length - 1] = 0;

        walk[walk.length - 1][0] = 0;
        walk[walk.length - 2][0] = 0;
        walk[walk.length - 1][1] = 0;

        walk[walk.length - 1][walk[0].length - 2] = 0;
        walk[walk.length - 2][walk[0].length - 1] = 0;
        walk[walk.length - 1][walk[0].length - 1] = 0;
        return walk;
    }

    public void dead(BomberServer b) {
    //int i = indexOf(b);
    //users[i].user().dead(true);
    //sendAll(b, 8, "");
    }

    public void walk(int x, int y, int i) {
        walk[x][y] = i;
    }

    public void powerUp(int x, int y) {
        int pu = new Random().nextInt(2);
        if (pu > 0) {
        //   sendAll(10, "" + (char) ((pu - 1) + 32) + (char) (x + 32) + (char) (y + 32));
        }
    }

    public void sendAll(int id, int cmd, byte[] b) {
        byte[] b2 = new byte[b.length];
        b2[0] = (byte)cmd;
        b2[1] = (byte)id;
        for (int i = 1; i < b.length - 1; i++) {
            b2[i + 1] = b[i];
        }
        for (int i = 0; i < users.length; i++) {
            if (users[i] != null) {
                connectors[i].send(b2);
            }
        }
    }
    
    public void sendAll(int[] all) {
        for (int i = 0; i < users.length; i++) {
            if (users[i] != null) {
                connectors[i].send(all);
            }
        }
    }
    
    public void sendAll(int cmd, byte[] b) {
        b[0] = (byte)cmd;
        for (int i = 0; i < users.length; i++) {
            if (users[i] != null) {
                connectors[i].send(b);
            }
        }
    }
}
